package org.arclib.examples.sprite;

import java.io.IOException;

import org.arclib.ArcGame;
import org.arclib.draw.Color;
import org.arclib.draw.Draw;
import org.arclib.math.Point;
import org.arclib.math.Size;
import org.arclib.sound.Sound;
import org.arclib.x.sprite.Sprite;
import org.arclib.x.sprite.frame.COLLISION_TYPE;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

public class SpriteTest extends ArcGame
{
	Sprite s1, s2;
	Sprite circleOne, circleTwo;
	Sprite boxOne, boxTwo;
	Point pivot = Point.Zero;
	double spriteAngle = 0;
	double spriteDist = 280;

	boolean circleOneDir = false;
	boolean circleTwoDir = true;
	boolean boxOneDir = false;
	boolean boxTwoDir = false;

	double zoomAmount = 1;
	boolean zIn = true;

	protected SpriteTest(String argT, Size argS, boolean argFS)
			throws IOException
	{
		super(argT, argS, argFS);
		// TODO Auto-generated constructor stub

		s1 = new Sprite(COLLISION_TYPE.CIRCLE);
		s2 = new Sprite(COLLISION_TYPE.BOX);

		circleOne = new Sprite(COLLISION_TYPE.CIRCLE);
		circleOne.addFrame("testbin/asteroid1.png", "static", -1, null);
		circleOne.setPosition(window.getWidth() / 2, window.getHeight() / 2);

		circleTwo = new Sprite(COLLISION_TYPE.CIRCLE);
		circleTwo.addFrame("testbin/btn1.png", "one", 500, null);
		circleTwo.addFrame("testbin/btn2.png", "one", 1000, null);
		circleTwo.setPosition(window.getWidth() / 2,
				window.getHeight() / 2 + 10);
		circleTwo.setPivot(circleTwo.getWidth(), circleTwo.getHeight());

		// ////////////////////////////////////////////////////////////////////////////
		boxOne = new Sprite(COLLISION_TYPE.BOX);
		boxOne.addFrame("testbin/p1.png", "one", 1000, null);
		boxOne.addFrame("testbin/p2.png", "one", 1000, null);
		boxOne.addFrame("testbin/p3.png", "one", 500, null);
		boxOne.addFrame("testbin/p4.png", "one", 500, new Sound(
				"testbin/laser.wav"));
		boxOne.setPosition(window.getWidth() / 2 + 200, window.getHeight() / 2);

		boxTwo = new Sprite(COLLISION_TYPE.BOX);

		boxTwo.addFrame("testbin/c11.png", "one", 1000, null);
		boxTwo.addFrame("testbin/c12.png", "one", 500, null);
		boxTwo.addFrame("testbin/c13.png", "one", 250, null);
		boxTwo.addFrame("testbin/c14.png", "one", 125, null);

		boxTwo.addFrame("testbin/c21.png", "two", 125, null);
		boxTwo.addFrame("testbin/c22.png", "two", 250, null);
		boxTwo.addFrame("testbin/c23.png", "two", 500, null);
		boxTwo.addFrame("testbin/c24.png", "two", 1000, null);

		boxTwo.setPosition(window.getWidth() / 2 - 200,
				window.getHeight() / 2 - 10);
		boxTwo.setPivot(circleTwo.getWidth(), circleTwo.getHeight());

		s1.addFrame("testbin/arrow.png", "static", -1, null);

		s2.addFrame("testbin/king.jpg", "static", -1, null);
		s2.setPosition(window.getWidth() / 2, window.getHeight() / 2);
		s2.addRotationPoint(-s2.getWidth() / 2, -s2.getHeight() / 2);

	}

	public void process()
	{
		Point mouse = new Point(Mouse.getX(), Mouse.getY());

		spriteAngle += .002;
		Point p = Point.Zero;

		s1.setPosition(p.fromPolar(spriteDist, spriteAngle).add(
				new Point(window.getWidth() / 2, window.getHeight() / 2)));
		s1.pointTo(window.getWidth() / 2, window.getHeight() / 2);

		s1.process();
		s1.draw();

		if (boxTwo.getAnimNum() == 3 && boxTwo.getAnim() == "one")
		{
			boxTwo.setAnim("two");
		} else if (boxTwo.getAnimNum() == 3 && boxTwo.getAnim() == "two")
		{
			boxTwo.setAnim("one");
		}

		if (zIn)
		{
			zoomAmount += .1;

			if (zoomAmount > 3)
				zIn = false;
		} else
		{
			zoomAmount -= .1;
			if (zoomAmount < 1)
				zIn = true;
		}

		circleOne.setZoom(zoomAmount);
		boxOne.setZoom(1, zoomAmount);

		// / circle 1 processing
		if (circleOneDir == false)
		{
			circleOne.setPosition(circleOne.getX() + 1, circleOne.getY());
			if (circleOne.getX() > window.getWidth())
				circleOneDir = true;
		} else if (circleOneDir == true)
		{
			circleOne.setPosition(circleOne.getX() - 1, circleOne.getY());
			if (circleOne.getX() < 0)
				circleOneDir = false;
		}

		// / circle two processing
		if (circleTwoDir == false)
		{
			circleTwo.setPosition(circleTwo.getX() + 1, circleTwo.getY());
			if (circleTwo.getX() > window.getWidth())
				circleTwoDir = true;
		} else if (circleTwoDir == true)
		{
			circleTwo.setPosition(circleTwo.getX() - 1, circleTwo.getY());
			if (circleTwo.getX() < 0)
				circleTwoDir = false;
		}

		if (circleTwo.collide(Mouse.getX(), Mouse.getY()))
		{
			circleTwo.setColor(Color.Red);
		} else
		{
			circleTwo.setColor(new Color(255, 255, 255, 255));
		}

		circleTwo.setAngleDeg(circleTwo.getAngleDeg() + 2);

		// ////////////////////////////////////////////////////////////////////////////////////////////

		// / circle 1 processing
		if (boxOneDir == false)
		{
			boxOne.setPosition(boxOne.getX() + 1, boxOne.getY());
			if (boxOne.getX() > window.getWidth())
				boxOneDir = true;
		} else if (boxOneDir == true)
		{
			boxOne.setPosition(boxOne.getX() - 1, boxOne.getY());
			if (boxOne.getX() < 0)
				boxOneDir = false;
		}

		// / circle two processing
		if (boxTwoDir == false)
		{
			boxTwo.setPosition(boxTwo.getX() + 1, boxTwo.getY());
			if (boxTwo.getX() > window.getWidth())
				boxTwoDir = true;
		} else if (boxTwoDir == true)
		{
			boxTwo.setPosition(boxTwo.getX() - 1, boxTwo.getY());
			if (boxTwo.getX() < 0)
				boxTwoDir = false;
		}

		boxTwo.setAngleDeg(boxTwo.getAngleDeg() + .5);

		// ///////////////////////////////////////////////////////////////////////

		// COLLISIONS //
		if (circleOne.collide(circleTwo))
		{
			circleOneDir = !circleOneDir;
		}

		if (circleOne.collide(boxTwo))
		{
			circleOneDir = !circleOneDir;
		}

		if (circleTwo.collide(boxOne))
		{
			circleTwoDir = !circleTwoDir;
		}

		if (circleTwo.collide(boxTwo))
		{
			circleTwoDir = !circleTwoDir;
		}

		if (boxOne.collide(boxTwo))
		{
			boxOneDir = !boxOneDir;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			pivot.x--;
		} else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			pivot.x++;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_UP))
		{
			pivot.y--;
		} else if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
		{
			pivot.y++;
		}

		s2.setPosition(new Point(Mouse.getX(), Mouse.getY()));
		s2.setPivot(pivot);
		s2.process();
		s2.draw();

		Draw.circle(s2.getRotationPoint(0), 10, 8, new Color(255, 0, 0, 100),
				true);

		// / set box1 and circle1 positions
		// boxOne.setPosition(arc.window.getWidth/2, arc.window.getHeight/2);
		// circleOne.setPosition(arc.input.mouseX, arc.input.mouseY);

		circleOne.process();
		circleTwo.process();
		boxOne.process();
		boxTwo.process();

		circleOne.draw();
		circleOne.drawBounds();

		circleTwo.draw();
		circleTwo.drawBounds();

		boxOne.draw();
		// boxOne.drawBounds();

		boxTwo.draw();
		// boxTwo.drawBounds();

		// strictly test circleOne against boxOne
		if (boxOne.collide(circleOne))
		{
			boxOneDir = !boxOneDir;
		}

		if (s1.collide(s2))
		{
			s2.setColor(Color.Red);
		} else
		{
			s2.setColor(new Color(255, 255, 255, 255));
		}

		if (Keyboard.isKeyDown('r'))
		{
			double angle = s2.getAngleDeg();
			angle++;
			s2.setAngleDeg(angle);
		}

	}

	public static void main(String[] args) throws IOException,
			InterruptedException
	{
		// TODO Auto-generated method stub
		SpriteTest t = new SpriteTest("Sprite Test", Size.d640x480, false);
		t.run();
	}
}
